package com.hongdee.insurance.company.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.DateUtils;
import com.hongdee.common.util.EntityUtils;
import com.hongdee.insurance.commission.service.ITCommissionSrevice;
import com.hongdee.insurance.company.entity.TInsuranceCompany;
import com.hongdee.insurance.company.service.ITInsuranceCompanyService;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Mr. Xu on 2017/4/24.
 */
@RestController
@RequestMapping("/insurance")
public class TInsuranceCompanyController {
    @Autowired
    private ITInsuranceCompanyService itInsuranceCompanyService;
    @Autowired
    private ITCommissionSrevice itCommissionSrevice;

    //测试
    @RequestMapping("/testLink")
    @ResponseBody
    public Map test(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token
    ) throws Exception {
        Map map = new HashMap();
        map.put("test", "Hello,world！");
        map.put("success", true);
        return map;
    }

    /**
     * 新增或修改保险公司
     *
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity Save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TInsuranceCompany data) throws Exception {
        EntityUtils.valid(data);
        TInsuranceCompany tBcCar = this.itInsuranceCompanyService.saveOrUpdate(data, uid);
        ResultEntity res = new ResultEntity();
        res.setData(tBcCar);
        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }

    /**
     * 保险公司分页查询
     *
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestBody Map data) throws Exception {
        Map map = itInsuranceCompanyService.page(uid, start, limit, data);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * 启用保险公司
     *
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/start")
    @ResponseBody
    public ResultEntity start(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestParam(name = "id", required = false) String id) throws Exception {
        String status = "1";
        String[] dl = id.split(",");
        for (String idtemp : dl) {
            this.itInsuranceCompanyService.update(idtemp, status);
        }
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("启用成功");
        return res;
    }

    /**
     * 停用保险公司
     *
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/stop")
    @ResponseBody
    public ResultEntity stop(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", required = false) String id) throws Exception {
        String status = "0";
        String[] dl = id.split(",");
        for (String idtemp : dl) {
            this.itInsuranceCompanyService.update(idtemp, status);
        }
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("停用成功");
        return res;
    }

    /**
     * 保险公司明细
     *
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择保险公司！");
        }
        TInsuranceCompany map = itInsuranceCompanyService.getInfo(id);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }

    /**
     * @return List
     * @throws Exception
     * @desc 保险公司下拉菜单
     * @author 术洪超
     */
    @RequestMapping("/combo")
    @ResponseBody
    public List<Map> combo(@RequestParam(name = "uid", defaultValue = "") String uid,
                           @RequestParam(name = "token", defaultValue = "") String token,
                           @RequestParam(name = "storeCode", defaultValue = "")String  storeCode) throws Exception {
        return this.itInsuranceCompanyService.combo(uid,storeCode);
    }

    /**
     * 获得保险公司信息 及佣金信息
     * @param uid
     * @param token
     * @param time
     * @param quotationId
     * @param companyId
     * @return
     * @throws Exception
     */
    @RequestMapping("/infos")
    @ResponseBody
    public ResultEntity infos(@RequestParam(name = "uid", defaultValue = "") String uid,
                     @RequestParam(name = "token", defaultValue = "") String token,
                     @RequestParam(name = "time", defaultValue = "") String time,
                     @RequestParam(name = "quotationId", defaultValue = "") String quotationId,
                     @RequestParam(name = "iscompulsoryinsurance", defaultValue = "") String iscompulsoryinsurance,
                     @RequestParam(name = "companyId", defaultValue = "") String companyId) throws Exception {
        ResultEntity res=new ResultEntity();
        if (StringUtils.isEmpty(companyId)) {
            throw new Exception("请选择保险公司!!");
        }
        if(StringUtils.isEmpty(quotationId)){
            throw new Exception("请提供报价单信息!!");
        }
        if("".equals(time)){
            time= DateUtils.formatLongTime(System.currentTimeMillis(),"yyyy-MM-dd");
        }
        Map map = itInsuranceCompanyService.info(companyId);
        Map<String,Object> data=this.itCommissionSrevice.getCarInsuranceCommissionById(quotationId,time,uid,iscompulsoryinsurance,companyId);
        data.put("company",map);
        res.setData(data);
        res.setSuccess(true);
        return res;
    }

    @RequestMapping("/companys")
    @ResponseBody
    public ResultEntity insuranceCompanys(@RequestParam(name = "uid", defaultValue = "") String uid,
                                          @RequestParam(name = "token", defaultValue = "") String token) throws Exception {
        ResultEntity res=new ResultEntity();
        List list=this.itInsuranceCompanyService.getCompanys();
        res.setData(list);
        res.setSuccess(true);
        res.setMsg("查询保险公司成功!!");
        return res;
    }

    /**
     * 获得保险公司合同号信息
     * @param uid uid
     * @param token token
     * @param companyId 4s店ID
     * @param time 出单日期
     * @param insuranceCompanyId 保险公司ID
     * @return
     * @throws Exception
     */
    @RequestMapping("/contract")
    @ResponseBody
    public Map insuranceContract(@RequestParam(name = "uid", defaultValue = "") String uid,
                                 @RequestParam(name = "token", defaultValue = "") String token,
                                 @RequestParam(name = "companyId", defaultValue = "") String companyId,
                                 @RequestParam(name = "time", defaultValue = "") String time,
                                 @RequestParam(name = "insuranceCompanyId",defaultValue = "")String insuranceCompanyId) throws Exception {
        Map map=new HashMap();
        map.put("success",true);
        map.put("msg","查询成功");
        if("".equals(time)){
            time= DateUtils.formatLongTime(System.currentTimeMillis(),"yyyy-MM-dd");
        }
        Map insuranceContractInfo=itInsuranceCompanyService.getInsuranceCompanyInfo(insuranceCompanyId,time,uid,companyId);
        map.put("data",insuranceContractInfo);
        return map;
    }

    /**
     * 根据出单日期获取保险公司的佣金费率信息
     * @param uid
     * @param token
     * @param time 出单日期
     * @param quotationId 报价单ID
     * @param iscompulsoryinsurance 保单类型 单交强 单商业 交商共保
     * @param companyId 4s店ID
     * @return
     * @throws Exception
     */
    @RequestMapping("/commission")
    @ResponseBody
    public Map insuranceCommission(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token,
                                   @RequestParam(name = "time", defaultValue = "") String time,
                                   @RequestParam(name = "quotationId", defaultValue = "") String quotationId,
                                   @RequestParam(name = "iscompulsoryinsurance", defaultValue = "") String iscompulsoryinsurance,
                                   @RequestParam(name = "companyId", defaultValue = "") String companyId) throws Exception {
        Map map=new HashMap();
        map.put("success",true);
        map.put("msg","查询成功");
        if("".equals(time)){
            time= DateUtils.formatLongTime(System.currentTimeMillis(),"yyyy-MM-dd");
        }
        Map<String,Object> data=this.itCommissionSrevice.getCarInsuranceCommissionById(quotationId,time,uid,iscompulsoryinsurance,companyId);
        map.put("data",data);
        return map;
    }



}
